﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DictManage.Code;

namespace DictManage
{
    public partial class FrmTopLevel : Form
    {
        public FrmTopLevel()
        {
            InitializeComponent();

        }
        string m_dicttypeId = "";
        public FrmTopLevel(string dicttypeId, string dicttypeName)
        {
            InitializeComponent();

            this.txt_pidId.Text = dicttypeId;
            m_dicttypeId = dicttypeId;
            this.txt_pidName.Text = dicttypeName;
            this.txt_pidName.Enabled = false;
            this.txt_pidId.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {//保存
            if (CheckInput())
            {
                string m_Id = Guid.NewGuid().ToString();
                string m_Name = this.txt_Name.Text;
                string m_Seq = this.txt_Seq.Text;
                string m_Remark = this.txt_Remark.Text;
                string pidId = "-1";
                if (this.txt_DictTypeID.Checked)
                {
                    pidId = "-1";
                }
                else
                {
                    pidId = this.txt_pidId.Text;
                    if (pidId.Length == 0) pidId = "-1";
                }
                if (this.Tag.ToString() == "添加")
                {
                    string sql = @"INSERT INTO [TB_DictType]([ID],[Name],[Remark],[Seq],[Editor],[LastUpdated],[PID])
                                            VALUES ('" + m_Id + "','" + m_Name + "','" + m_Remark + "','" + m_Seq + "','','" + DateTime.Now.ToString() + "','" + pidId + "')";
                    OleDbAccess.ExecuteNonQury(sql);
                    MessageBox.Show("添加大类字典成功！", "系统提示");
                }
                else if (this.Tag.ToString() == "编辑")
                {
                    string sql_upp = @"UPDATE [TB_DictType] SET  [Name] = '" + m_Name + "' ,[Remark] = '" + m_Remark + "' ,[Seq] = '" + m_Seq + "',[PID] = '" + pidId + "' WHERE id='" + this.label4.Text + "'";

                    OleDbAccess.ExecuteNonQury(sql_upp);
                    MessageBox.Show("修改大类字典成功！", "系统提示");
                }
                //this.Close();
            }
        }
        private bool CheckInput()
        {
            bool flg = true;
            StringBuilder list = new StringBuilder();
            if (this.txt_Name.Text.Length == 0)
            {
                list.AppendLine(" ● 项目名称不能为空！");
                flg = true;
            }
            else if (this.txt_Seq.Text.Length == 0)
            {
                this.txt_Seq.Text = "0";
            }
            if (list.Length > 0)
                MessageBox.Show(list.ToString(), "系统提示");
            return flg;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void FrmTopLevel_Load(object sender, EventArgs e)
        {
            if (this.Tag.ToString() == "编辑")
            {
                string pidName = "";
                string sql = "select * from TB_DictType where id='" + this.txt_pidId.Text + "'";
                DataTable table = OleDbAccess.GetDataSet(sql).Tables[0];
                try
                {
                    pidName = OleDbAccess.GetDataSet("select * from TB_DictType where id='" + table.Rows[0]["Pid"].ToString() + "'").Tables[0].Rows[0]["Name"].ToString();
                }
                catch
                {
                    pidName = "无（顶级目录）";
                    this.txt_DictTypeID.Checked = true;
                }
                this.label4.Text = table.Rows[0]["Id"].ToString();
                this.txt_Name.Text = table.Rows[0]["Name"].ToString();
                this.txt_Seq.Text = table.Rows[0]["Seq"].ToString();
                this.txt_pidId.Text = table.Rows[0]["Pid"].ToString();
                this.txt_pidName.Text = pidName;
                this.txt_Remark.Text = table.Rows[0]["Remark"].ToString();
            }
            this.label4.Visible = false;
        }

        private void txt_DictTypeID_Click(object sender, EventArgs e)
        {
            if (txt_DictTypeID.Checked)
            {
                this.txt_pidName.Text = "无（顶级目录）";
                this.txt_DictTypeID.Checked = true;
            }
            else
            {
                FrmTopLevel_Load(sender, e);
            }
        }
    }
}
